package com.code.app.loading;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.LinearLayout;

import com.code.app.R;

public class LoadingView extends LinearLayout {

    private float animDistance;

    private ShapeView shapeView;

    private View shadowView;

    private boolean animDoing = true;

    public LoadingView(Context context) {
        super(context);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.layout_loading, this);
        shapeView = findViewById(R.id.shape);
        shadowView = findViewById(R.id.shadow_view);
        animDistance = dip2px(80);
        dropAnim();
    }

    private float dip2px(int dip) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics());
    }


    public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void dropAnim() {
        if (!animDoing) {
            return;
        }
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(shapeView, "translationY", 0, animDistance);
        objectAnimator.setInterpolator(new AccelerateInterpolator());
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                upAnim();
            }
        });

        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(shadowView, "scaleX", 1f, 2f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(objectAnimator, objectAnimator1);
        animatorSet.setDuration(350);
        animatorSet.start();


    }

    private void upAnim() {
        if (!animDoing) {
            return;
        }
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(shapeView, "translationY", animDistance, 0);
        objectAnimator.setInterpolator(new AccelerateInterpolator());
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                shapeView.exchange();
                dropAnim();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                startRotation();
            }
        });


        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(shadowView, "scaleX", 2f, 1f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(objectAnimator, objectAnimator2);
        animatorSet.setDuration(350);
        animatorSet.start();
    }

    private void startRotation() {
        ObjectAnimator objectAnimator = null;
        ShapeView.Shap shap = shapeView.getShap();
        if (shap.equals(ShapeView.Shap.RECTF)) {
            objectAnimator = ObjectAnimator.ofFloat(shapeView, "rotation", 0f, 180f);
        } else {
            objectAnimator = ObjectAnimator.ofFloat(shapeView, "rotation", 0f, 120f);
        }
        objectAnimator.setDuration(350);
        objectAnimator.setInterpolator(new AccelerateInterpolator());
        objectAnimator.start();
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        setVisibility(View.INVISIBLE);

        shapeView.clearAnimation();
        shadowView.clearAnimation();

        ViewGroup viewGroup = (ViewGroup) getParent();
        if (viewGroup != null) {
            viewGroup.removeView(this);
            this.removeAllViews();
        }
        animDoing = false;
    }
}
